package com.itheima.Servlet.util;

import com.itheima.domain.Student;

import javax.servlet.ServletContext;
import java.io.*;
import java.util.ArrayList;

public class studentIDUtil {
   private studentIDUtil() {
    }
    public static String getId(ServletContext context){
        String realPath = context.getRealPath("/stu.txt");
            //创建字符缓冲输入流 读取关联的文件

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(realPath),"UTF-8"));
            //创建集合对象
            ArrayList<Student>list = new ArrayList<Student>();
            String line;
            while ((line=br.readLine())!=null){
                String[] split = line.split(",");
                Student stu = new Student();
                stu.setSid(split[0]);
                stu.setUsername(split[1]);
                stu.setAge(Integer.parseInt(split[2]));
                stu.setScore(Integer.parseInt(split[3]));
                //添加到集合中
                list.add(stu);
            }
            if (list.size()==0) {
                return "1";
            }

            String sid = list.get(list.size() - 1).getSid();
            return Integer.parseInt(sid)+1 +"";


        }
         catch (Exception e) {

        }
        return  null;

    }
}
